package edu.lingnan.rili.controller;


import edu.lingnan.rili.pojo.Uploading;
import edu.lingnan.rili.service.UploadingService;
import edu.lingnan.rili.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import edu.lingnan.rili.pojo.Match;
import edu.lingnan.rili.service.UploadingService;
import edu.lingnan.rili.utils.Result;
import edu.lingnan.rili.vo.OtherBonusPointsViewPages;
import edu.lingnan.rili.vo.PersonalInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import static edu.lingnan.rili.controller.AdministratorController.isAdminLogin;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈世杰
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/uploading")
@Api(tags = "文件上传控制器")
public class UploadingController {
    @Autowired
    private UploadingService uploadingService;

    /**
     * @description 通过uploadingId查询文件信息
     * @author xBaozi
     * @date 22:36 2022/3/14
     **/
    @GetMapping("/{id}")
    @ApiOperation("通过uploadingId查询文件信息")
    public Result<Uploading> getById(@ApiParam(value = "所上传文件的id") @PathVariable String id,
                                     @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        Result<Uploading> result = new Result<>();
        Uploading uploading = uploadingService.selectById(id, Uploading.EXAMINATION_PASSED);
        if (!ObjectUtils.isEmpty(uploading)) {
            result.setResultCode(200);
            result.setData(uploading);
            result.setMessage("查询成功");
        } else {
            result.setResultCode(201);
            result.setMessage("查询失败");
        }
        return result;
    }

    @GetMapping
    @ApiOperation("获取未审核数据的UploadingId")
    public Result<List<Object>> getIdNotReviewed(@ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        Result<List<Object>> result = new Result<>(201, "查询失败，未找到有效数据");
        List<Object> uploadingIdNotReviewed = uploadingService.getUploadingIdNotReviewed();
        if (uploadingIdNotReviewed != null) {
            result.setResultCode(200);
            result.setMessage("查询成功，共" + uploadingIdNotReviewed.size() + "条数据待审核");
            result.setData(uploadingIdNotReviewed);
        }
        return result;
    }

    /**
     * 这是一个提供模拟用户登录后数据的controller
     *
     * @param session
     * @return
     */
    @PostMapping("/setUp")
    public Result setUp(String userName, String userId, String userCollege, String userMajor,
                        String userClass, String userGrade, @ApiIgnore HttpSession session) {

        //创建一个模拟数据
        Match match = new Match(userId, "e10adc3949ba59abbe56e057f20f883e", userName, userCollege, userGrade, userMajor, userClass);
        //将模拟数据添加到session中
        session.setAttribute("match", match);

        Result result = new Result();
        result.setData(match);
        result.setResultCode(200);
        result.setMessage("设置测试数据成功");
        return result;
    }

    /**
     * 上传文件，可重复使用(此controller已用其他代码代替)
     *
     * @param file 图片
     * @return 返回一个json，其中包括图片在服务器的访问地址（回显）
     */
    @PostMapping("/uploadPhoto")
    public Result uploadPhoto
    (@RequestParam(value = "file", required = false) MultipartFile file,
     HttpSession session) throws IOException {

        Result result = uploadingService.uploadingPhoto(file, session);

        return result;
    }

    /**
     * 其他加分项页面上传controller
     *
     * @param file        图片
     * @param projectName 项目名称
     * @param projectType 加分类型
     * @return 返回一个json
     */
    @PostMapping("/otherBonusUpload")
    public Result otherBonusUpload
    (@RequestParam(value = "file", required = false) MultipartFile file,
     String projectName, String projectType,
     Double score, HttpSession session) {
        System.out.println("++++++++++"+file);
        System.out.println("-----------------"+projectName+projectType+score);
        return uploadingService.otherBonusUpload(file, projectName, projectType, score, session);
    }

    /**
     * 其他比赛奖项页面上传controller
     *
     * @param file         图片
     * @param matchName    比赛名称
     * @param prizeAwarded 比赛名次
     * @return 返回一个json
     */
    @PostMapping("/otherMatchUpload")
    public Result otherMatchUpload
    (@RequestParam(value = "file", required = false) MultipartFile file,
     String matchName, String projectType,
     String prizeAwarded, Double score, HttpSession session) {
        return uploadingService.otherMatchUpload
                (file, matchName, projectType, score, prizeAwarded, session);
    }

    /**
     * 这是一个查询其他加分项申请记录的controller
     *
     * @return
     */
    @GetMapping("/queryOtherExtraPointsApplyRecord")
    public Result<List<OtherBonusPointsViewPages>>
    queryOtherExtraPointsApplyRecord(HttpSession session) {

        return uploadingService.queryOtherExtraPointsApplyRecord(session);
    }

    /**
     * 这是一个查询其他比赛申请记录的controller
     *
     * @return
     */
    @GetMapping("/queryOtherGamesApplyRecord")
    public Result<List<OtherBonusPointsViewPages>> queryOtherGamesApplyRecord(HttpSession session) {

        return uploadingService.queryOtherGamesApplyRecord(session);
    }

    /**
     * 这是一个查看个人上传记录的controller
     * (显示证明材料这些)
     *
     * @param session
     * @return
     */
    @GetMapping("/queryingPersonalUploadingRecords")
    public Result<PersonalInformation> queryingPersonalUploadingRecords(HttpSession session, String uploadingId) {
        return uploadingService.queryingPersonalUploadingRecords(session, uploadingId);
    }
}

